package com.situ.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bean.Product;
import com.service.IProductService;
import com.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    IProductService productService;

    @PostMapping("/")
    public ResultData insert(@RequestBody Product product){

        return ResultData.success(productService.inseret(product));
    }

    @PutMapping("/")
    public  ResultData  update(@RequestBody Product product){
         Product data=productService.update(product);
         return ResultData.success(data);
    }


    @GetMapping("/{id}")
    public ResultData getById(@PathVariable Integer id){
        Product data=productService.getById(id);
        return ResultData.success(data);
    }


    @DeleteMapping("/{id}")
    public ResultData deleteById(@PathVariable Integer id){
        int data=productService.deleteById(id);
       if(data>0){
           return ResultData.success();
       }else{
           return ResultData.fail500();
       }
    }

    @GetMapping("/page")
    public ResultData getListByPage(Page page, @RequestBody  Product product){
        IPage<Product> data= (IPage<Product>) productService.getListByPage(page,product);
         return ResultData.success(data);
    }

  @GetMapping("/list")
    public ResultData getList(@RequestBody Product product){
        List<Product> data=productService.getList(product);
        return ResultData.success(data);
    }







}
